'''
Created on Aug 13, 2012

@author: petrina
'''

from plot import Plot
from csv_export import CSV_export
import logging


class Output_manager(object):
    '''
    manages generating plots and csv-files using an Indicator_list
    '''


    def __init__(self, indilist, exp_plot = True, exp_csv = True, exp_all2csv=True,input = None):
        '''
        @param indilist: Indicator_list-object (list containing the indicators for the export)
        @param exp_plot: boolean value indicating whether to generate an export plots of the indicators
        @param exp_csv: boolean value indicating whether to write csv-files
        '''
        
        self._indilist = indilist
        self._exp_csv = exp_csv
        self._exp_all2csv = exp_all2csv
        self._exp_plot = exp_plot
        self._input = input
        
        logging.debug("Output_manager: created Output_manager")
    
    
    #----------------------private methods of the class------------------------
        
        
    def _plot_indis(self):
        '''
        plots indicators which are in the indicator-list
        '''
        
        for indi in self._indilist:
            try:
                indi.plot(Plot())
            except Exception, e:
                logging.error("Output_manager: error at plotting indicators - ignoring indicator " + indi.name + ": " + e.__str__())
    

    
    #----------------------public methods of the class-------------------------
   
    def export_plot(self):
        '''
        export plot of all indicators in the list (if the configuration suggest the export of plots).
        '''
        
        if self._exp_plot:
            logging.info("Output_manager: starting to create and export plots")   
            self._plot_indis()
      
        else:
            logging.info("Output_manager: no plots are created because of the export configuration")


    def plot_data(self):
        if self._input is not None:
            self._input.plot(Plot())
        
    def export_csv(self, delimiter = ','):
        '''
        exports csv-files for all indicators in the list (if the configuration suggest the export of csv-files)
        '''
        
        if self._exp_csv:
            logging.info("Output_manager: starting to export csv files")
            # each indicator knows how export itself
            for indi in self._indilist:
                try:
                    indi.export2csv(CSV_export(delimiter))
                except Exception, e:
                    logging.error("Output_manager: cannot export indicator " +indi.name + " to csv: " + e.__str__())
        
        else:
            logging.info("Output_manager: no single csv-files are created because of the export configuration")
        
        if self._exp_all2csv:
            self._indilist.create_all_indicators_csv(CSV_export(delimiter))
    
        else:
            logging.info("Output_manager: no csv-file with all indicators is created because of the export configuration")
        
